package pro.spring.controller.frontController;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.codehaus.jackson.map.SerializationConfig;

import pro.spring.wesocketController.MyMessageInbound;
import pro.spring.model.Theme;
import pro.spring.model.Udiscuss;
import pro.spring.model.Udiscusser;


@Controller
@RequestMapping("them")
public class ThemController extends BaseController{
	
	private String local = "page/front/them/";
	
	private Integer pagesize = 10 ;
	
	@RequestMapping(value="index.html",method=RequestMethod.GET)
	public String index(ModelMap map,HttpServletRequest request,
			HttpServletResponse response,String key){
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");
		map.put("pages",themeService.getPaginationInfo(key,uId,pagesize));
		return local+"index";
	}
	
	@RequestMapping(value="new.html",method=RequestMethod.GET)
	public String newo(ModelMap map,HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");
		map.put("user",usersService.selectByPrimaryKey(uId));
		return local+"addone";
	}
	
	@RequestMapping(value="reli.html",method=RequestMethod.GET)
	public String list(ModelMap map,@RequestParam(value = "p", defaultValue = "1")Integer pagenum,HttpServletRequest request,
			HttpServletResponse response,String key){
//		HttpSession session=request.getSession();
//		Integer uId = (Integer)session.getAttribute("uId");
		map.put("pages",themeService.getPaginationInfo(key,pagenum, pagesize));
		map.put("key",key);
		return local+"themli";
	}
	
	@RequestMapping(value="zhuti.html",method=RequestMethod.GET)
	public String zhuti(ModelMap map,@RequestParam(value = "p", defaultValue = "1")Integer pagenum,HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");
		map.put("pages",themeService.getPaginationByuser(uId,pagenum, pagesize));
		return local+"themli";
	}
	
	@ResponseBody
	@RequestMapping(value="add.json",method=RequestMethod.POST)
	public Integer add(Theme th,HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");
		return themeService.insertOnethem(uId,th);
	}
	
	@ResponseBody
	@RequestMapping(value="name.json",method=RequestMethod.POST)
	public Integer names(@RequestParam(value = "name")String name){
		Integer num = themeService.selectName(name);
		System.out.println(name+"nihaoh"+num);
		return num;
	}
	
	@ResponseBody
	@RequestMapping(value="addyes.json",method=RequestMethod.POST)
	public Integer add(Udiscuss ud,HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");
		ObjectMapper mapper = new ObjectMapper();
		mapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);
        mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
		ud.setUsersUId(uId);
		int num = udiscussService.insert(ud);
		if(num == 1){
			ud.setUsers(usersService.selectByPrimaryKey(uId));
			ud.setDsTime(new Date());
			ud.setDsContent(ud.getDsContent());
			try {
				String jsons = mapper.writeValueAsString(ud);
				Integer udid = ud.getUdiscusserDrId();
				MyMessageInbound.addjson(jsons,udid);
			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return num;
	}
	
	
	@RequestMapping(value="themone.html",method=RequestMethod.GET)
	public String themone(Integer id,ModelMap map,HttpServletRequest request,
			HttpServletResponse response,@RequestParam(value = "p", defaultValue = "10")Integer page){
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");
		Udiscusser u = udiscusserService.selectByPrimaryKeys(uId,id,page);
		Integer d = udiscusserService.selectCountBydrId(u.getDrId());
		System.out.println("aaaa"+d);
		if(uId==null){
			map.put("yonghu",0);
		}else{
			map.put("yonghu",1);
		}
		if(d>page){
			map.put("pageif",1);
		}else{
			map.put("pageif",0);
		}
		map.put("page",page);
		map.put("one",u);
		map.put("uid",uId);
		return local+"themone";
	}
	
	@ResponseBody
	@RequestMapping(value="zan.json",method=RequestMethod.POST)
	public Integer dianzan(Integer id,ModelMap map,HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session=request.getSession();
		Integer uId = (Integer)session.getAttribute("uId");
		return usupportService.dianzhan(uId, id);
	}
	
	
}
